package cn.fl.risknew.service.impl;


import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.risknew.condition.RskCommRecQueryCondition;
import cn.fl.risknew.constant.RiskConstant;
import cn.fl.risknew.dto.RskCommRecDTO;
import cn.fl.risknew.service.IRskCommRecNewApiService;
import cn.fl.risknew.service.IRskCommRecNewService;
import cn.fl.risknew.transform.RskCommRecVOTransform;
import cn.fl.risknew.vo.RskCommRecVO;
import fl.customer.facade.customer.api.ICustomerQueryApiService;
import fl.customer.facade.customer.dto.CstContactInfoDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: 沟通记录表 客户端服务层接口实现
 * 业务层方法名称前缀：
 * 新增：add*
 * 删除：remove*
 * 修改：modify*
 * 批量：batch*
 * 查询对象：select* get*
 * 查询集合：search*
 */
@Service
public class RskCommRecNewServiceImpl implements IRskCommRecNewService {

    /**
     * 日志
     */
    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(RskCommRecNewServiceImpl.class);

    /**
     * API 暴露服务
     */
    @Autowired
    private IRskCommRecNewApiService rskCommRecNewApiService;

    /**
     * 数据字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private ISysUsrApiService sysUsrApiService;
    @Autowired
    private ICustomerQueryApiService customerQueryApiService;
    @Autowired
    private ISysOrgApiService sysOrgApiService;

    @Override
    public RskCommRecVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) return null;
        RskCommRecDTO dto = this.rskCommRecNewApiService.selectRskCommRecById(id);
        return RskCommRecVOTransform.toVO(dto);
    }

    @Override
    public Long add(RskCommRecVO entity) throws BaseException {
        if (null == entity) return null;
        return this.rskCommRecNewApiService.addRskCommRec(RskCommRecVOTransform.toDTO(entity));
    }

    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) return false;
        return this.rskCommRecNewApiService.removeRskCommRecByIds(ids, CurrentThreadContext.getCurrentUserId());
    }

    @Override
    public boolean modify(RskCommRecVO entity) throws BaseException {
        if (null == entity) return false;
        return this.rskCommRecNewApiService.modifyRskCommRec(RskCommRecVOTransform.toDTO(entity));
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        RskCommRecQueryCondition condition = null;
        if (null == page) {
            page = new Pager();
            page.setCondition(new RskCommRecQueryCondition());
        }
        condition = (RskCommRecQueryCondition) page.getCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        page = this.rskCommRecNewApiService.searchRskCommRecListPage(page);
        if (CollectionUtils.isNotEmpty(page.getResultList())) {
            List<RskCommRecVO> voList = RskCommRecVOTransform.toVOList((List<RskCommRecDTO>) page.getResultList());
            this.setDictData(voList);
            page.setResultList(voList);
        }
        return page;
    }

    @Override
    public List<RskCommRecVO> searchList(RskCommRecQueryCondition condition) throws BaseException {
        if (null == condition) condition = new RskCommRecQueryCondition();
        condition.setSysCd(CurrentThreadContext.getCurrentSysCd());//当前系统代码
        condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());//当前机构代码
        condition.setUsrId(CurrentThreadContext.getCurrentUserId());//当前用户ID
        List<RskCommRecVO> voList = RskCommRecVOTransform.toVOList(this.rskCommRecNewApiService.searchRskCommRecList(condition));
        this.setDictData(voList);
        return voList;
    }

    /**
     * 获取催收方式
     *
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> getCollectWayCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(RiskConstant.CODE_COLLECT_WAY_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }
    
    /**
     * 获取外呼结果
     *
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> getCommStsCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(RiskConstant.CODE_COMM_STS_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 获取催收结果
     *
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> getCollectRstCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(RiskConstant.CODE_COLLECT_RST_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 获取欠款原因
     *
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> getOvdReasonCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(RiskConstant.CODE_OVD_REASON_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 获取客户还款方式
     *
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> getPayTypCdList() throws BaseException {
        return this.sysDictionaryApiService.getKvList(RiskConstant.CODE_PAY_TYP_CD, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
    }

    /**
     * 添加数据字典名称
     *
     * @param voList
     */
    public void setDictData(List<RskCommRecVO> voList) {
        if (CollectionUtils.isEmpty(voList)) return;
        //催收方式
        Map<String, String> collectWayCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), RiskConstant.CODE_COLLECT_WAY_CD);
        //催收结果
        Map<String, String> collectRstCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), RiskConstant.CODE_COLLECT_RST_CD);
        //欠款原因
        Map<String, String> reasonCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), RiskConstant.CODE_OVD_REASON_CD);
        //沟通状态
        Map<String, String> commStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), RiskConstant.CODE_COMM_STS_CD);

        List<SysOrgDTO> orgList = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getCurrentSysCd());
        Map<String, String> orgMap = null;
        if (CollectionUtils.isNotEmpty(orgList)) {
            orgMap = new HashMap<>(orgList.size());
            for (SysOrgDTO orgDTO : orgList) {
                orgMap.put(orgDTO.getOrgCd(), orgDTO.getOrgNm());
            }
        }

        for (RskCommRecVO vo : voList) {
            if (collectWayCdMap != null && collectWayCdMap.containsKey(vo.getCollectWayCd())) {
                vo.setCollectWayCdNm(collectWayCdMap.get(vo.getCollectWayCd()));
            }
            if (collectRstCdMap != null && collectRstCdMap.containsKey(vo.getCollectRstCd())) {
                vo.setCollectRstCdNm(collectRstCdMap.get(vo.getCollectRstCd()));
            }
            String ovdReasonCd = vo.getOvdReasonCd();
            String ovdReasonCdNm = "";
            if(StringUtils.isNotEmpty(ovdReasonCd)){
                String[] split = ovdReasonCd.split(";");
                Iterator<String> iterator = Arrays.asList(split).iterator();
                while(iterator.hasNext()){
                    String next = iterator.next();
                    ovdReasonCdNm += reasonCdMap.get(next);
                    if(iterator.hasNext()){
                        ovdReasonCdNm +=";";
                    }
                }
            }
            if(StringUtils.isNotEmpty(ovdReasonCdNm)){
                vo.setOvdReasonCdNm(ovdReasonCdNm);
            }
            if (orgMap != null && orgMap.containsKey(vo.getBuOrgCd())) {
                vo.setBuOrgCdNm(orgMap.get(vo.getBuOrgCd()));
            }
            if (commStsCdMap != null && commStsCdMap.containsKey(vo.getCommStsCd())) {
                vo.setCommStsCd(commStsCdMap.get(vo.getCommStsCd()));
            }
            
            if (vo.getCommSec() != null && vo.getCommSec()>0) {
            	vo.setCommSecDtl(DateUtils.parseIntToDate(vo.getCommSec()));//通话时长格式化
            }
            
        }
    }

    /**
     * 根据客户ID查询客户联系人信息
     *
     * @param idList
     * @return
     */
    @Override
    public List<CstContactInfoDTO> searchCstLkMan(List<Long> idList) throws BaseException {
        return customerQueryApiService.searchCstContactInfo(idList);
    }

    @Override
    public String searchPreLoanRskInfo(Long cstId) {
        return rskCommRecNewApiService.searchPreLoanRskInfo(cstId);
    }
}
